"use strict";
uk.states.main = {
	preload: function(){
		/** 关卡信息 */
		uk.netbind(uk.netevents.STATIONLIST,this.on_station_list,this);
	},
	
	create: function(){
		this.bg = this.game.add.sprite(0,0,'playbg');
		this.bg.width = this.stage.width;
		this.bg.height = this.stage.height;
		console.log('main create');	
		uk.netcache_enable(false);
		uk.netsend(uk.netevents.STATIONLIST,{});
	},

	shutdown:function(){
		/**解绑和我有关的所有消息 */
		uk.netunbind(null,this);
	},

	on_station_list:function(obj){
		console.log('on_station_list',obj);
		/** 保存关卡信息 */
		this.stations = obj;
		
		for(var i in this.stations){
			var s = this.stations[i];
			s.btn = uk.game.add.button(0,0,'btn_play',function(){},this,1,0);
			s.btn.events.onInputUp.add(this.on_click_station,this,0,s);

			var txt = uk.game.add.text(0,0,s.name,uk.uidata.fontstyle.stationname);
			s.btn.addChild(txt);
			s.btn.ignoreChildInput = true;
		}
		this.refresh_station_layout();
	},

	on_click_station:function(uiitem,evt,isOver,s){
		if(!isOver) return;
		s.btn.visible = false;
		s.btn.frame = 0;
		uk.netsend(uk.netevents.STATIONSEL,{id:s.id});
	},

	refresh_station_layout:function(){
		var st = {x:160,y:200};
		var w = 0;
		for(var i in this.stations){
			var s = this.stations[i];
			w += s.btn.width;
		}
		
		st.x += (500-w)/2;
		for(var i in this.stations){
			var s = this.stations[i];
			s.btn.x = st.x;
			s.btn.y = st.y;
			st.x += 130;
		}
	},

	update: function(){
	},

	render:function(){
		var role = uk.logic.self;
		this.game.debug.text(role.name,5,15,'rgb(0,255,0)');
		this.game.debug.text(role.template.name,5,35,'rgb(0,255,0)');
		var y = 55;
		for(var i in role.props){
			this.game.debug.text(i+':'+role.props[i],5,y,'rgb(0,255,0)');
			y+=20;
		}

		for(var i in role.handcards){

		}
	}
};